package p.ithorns.framework.common.time;

import java.time.DateTimeException;
import java.time.LocalDateTime;

/**
 * TimeSlot
 * TODO ithorns
 *
 * @author Ht.L
 * @version 1.0.0
 * @date 2023/4/13 21:32
 */
public class TimeSlot implements Comparable<TimeSlot> {

    /**
     * 开始时间
     */
    private LocalDateTime start;

    /**
     * 结束时间
     */
    private LocalDateTime end;

    public TimeSlot() {
    }

    public TimeSlot(LocalDateTime start, LocalDateTime end) {
        if (start.isAfter(end)) {
            throw new DateTimeException("开始时间不能小于结束时间");
        }
        this.start = start;
        this.end = end;
    }

    @Override
    public int compareTo(TimeSlot o) {
        LocalDateTime otherStart = o.getStart();
        LocalDateTime otherEnd = o.getEnd();

        // 当前时间段在其他时间段后面
        if (start.isAfter(otherEnd)) {
            return 1;
        }

        // 当前时间段在其他时间段前面
        if (end.isBefore(otherStart)) {
            return -1;
        }

        // 时间重叠
        return 0;
    }

    public LocalDateTime getStart() {
        return start;
    }

    public void setStart(LocalDateTime start) {
        this.start = start;
    }

    public LocalDateTime getEnd() {
        return end;
    }

    public void setEnd(LocalDateTime end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return TimeUtil.toString(start) + "&" + TimeUtil.toString(end);
    }

    public static void main(String[] args) {
        LocalDateTime t1 = LocalDateTime.of(2023, 4, 13, 8, 30);
        LocalDateTime t2 = LocalDateTime.of(2023, 4, 13, 10, 30, 5);
        LocalDateTime t3 = LocalDateTime.of(2023, 4, 13, 10, 30, 3);
        LocalDateTime t4 = LocalDateTime.of(2023, 4, 13, 11, 30);

        TimeSlot ts1 = new TimeSlot(t1, t2);
        TimeSlot ts2 = new TimeSlot(t3, t4);
        System.out.println(ts1);

        System.out.println(ts1.compareTo(ts2));
    }
}